home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13742 < prev    next >
Encoding:
Text File  |  1996-08-05  |  9.1 KB  |  279 lines

  1. Path: news.ucdavis.edu!usenet
  2. From: Mike Ransom <mlransom@ucdavis.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Streams problem--simple? (help debug)
  5. Date: Tue, 26 Mar 1996 17:41:16 -0800
  6. Organization: UC Davis, Dept. of Agronomy & Range Science
  7. Message-ID: <31589CBC.7AD9@ucdavis.edu>
  8. NNTP-Posting-Host: zippy.ucdavis.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Sorry to start out my 1st post with something so big.  Hopefully by
  15. helping me, you will help others as well.  TIA
  16.  
  17. Could someone please comment on (and degbug :) ) the following program?
  18. I'm just starting with C++ and am trying to be a good boy and learn the
  19. streams way of doing things.  I've pared out some non-essential code for
  20. this posting.
  21.  
  22. The program is fairly simple.  It reads a tabular ASCII file of numbers
  23. and outputs same, after some calculations.  In this example, I'm reading
  24. lines into a string stream buffer, which I know isn't necessary for this
  25. sample, but I need to work with such a buffer for other purposes in
  26. other processing.
  27.  
  28. The problem behavior is that the the program will run correctly the
  29. first time after it is compiled, but then apparently fail to read the
  30. input file correctly in subsequent runs.  The subsequent executions will
  31. output the correct number of lines (showing a counter value) for the
  32. input file, but not the values I've attempted to calculate/output.  This
  33. is a little exasperating, since the program exits normally (I think) and
  34. I've explicitly closed the files--and everything should go out of scope
  35. anyway right???  This is running in a DOS window under Windows.  I'm
  36. using the Turbo C++ 3.0 for DOS compiler (that isn't the problem is
  37. it???).  I've tried it in DOS too, under DOS 5.0 and 6.22.  Sometimes it
  38. will crash the computer.
  39.  
  40. I suspect that my problem is related to EOL or EOF problems or get and
  41. put pointer locations.  I wasn't sure what the conventional way of
  42. extracting the EOL character from the input stream is; I took the
  43. approach of using the "getline" function.  As an aside you can see from
  44. comments, I was having problems with setting the default output width
  45. and with setting numerical precision of 0 decimal places.
  46.  
  47. /**********************************************************
  48. SAMPLES OF INPUT, OUTPUT:
  49.  
  50.  
  51. INPUT FILE:
  52.  
  53.     1  1   6.40  40.40  14.68   3.45   3.96   9.06   1.42
  54.     1  2   7.45  72.57  13.44   2.55   4.54   8.97   1.59
  55.     1  3   5.30  27.58  18.65   5.45   7.45   9.54   1.68
  56.     1  4   2.75  14.07  26.04  10.42  11.42  10.75   1.97
  57.     1  5   3.65  27.82  30.34  13.18  12.90  11.60   2.46
  58.     1  6   2.15   2.25  34.44  15.48  14.65  12.02   2.51
  59.     1  7    .20    .22  36.45  16.73  14.85  12.93   2.23
  60.     1  8   1.15   2.80  35.15  15.85  12.92  12.48   2.09
  61.     1  9   3.70  12.61  30.89  13.61   9.52  11.96   1.79
  62.     1 10   2.35   7.86  25.15   9.69   7.16  11.08   1.37
  63.     1 11   2.65   9.83  20.05   6.55   5.20  10.10   1.30
  64.     1 12   6.60  30.29  16.02   4.10   3.73   9.39   1.72
  65.  
  66.  
  67. SCREEN OUTPUT OF 1ST RUN
  68.  
  69. [WIN] 14:09:47.66 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
  70. Using input file x.x
  71. Writing output file x
  72.     1*   1  1  40.4   4.0   9.1  0.25    5.07    1.43    1.43    11.4   -29.0
  73.     2*   1  2  72.6   4.5   8.0  0.18   12.83    1.63    1.63     9.2   -63.3
  74.     3*   1  3  27.6   7.4  12.0  0.45    1.89    2.68    1.89    39.2    11.6
  75.     4*   1  4  14.1  11.4  18.2  1.02    0.43    4.11    0.43   135.1   121.1
  76.     5*   1  5  27.8  12.9  21.8  1.45    0.60    4.64    0.60   216.5   188.7
  77.     6*   1  6   2.3  14.6  25.0  1.96    0.04    5.27    0.04   333.2   330.9
  78.     7*   1  7   0.2  14.9  26.6  2.19    0.00    5.35    0.00   377.2   377.0
  79.     8*   1  8   2.8  12.9  25.5  2.02    0.04    4.65    0.04   302.4   299.6
  80.     9*   1  9  12.6   9.5  22.3  1.49    0.26    3.43    0.26   164.6   151.9
  81.    10*   1 10   7.9   7.2  17.4  0.88    0.28    2.58    0.28    73.3    65.5
  82.    11*   1 11   9.8   5.2  13.3  0.52    0.59    1.87    0.59    31.2    21.4
  83.    12*   1 12  30.3   3.7  10.1  0.29    3.19    1.34    1.34    12.7   -17.6
  84.  
  85. [WIN] 14:10:11.33 G:\MIKE\SAMSUL\ANALYSIS >
  86.  
  87.  
  88. SCREEN OUTPUT OF SUBSEQUENT RUNS
  89.  
  90. [WIN] 14:15:06.72 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
  91. Using input file x.x
  92. Writing output file x
  93.     1*
  94.     2*
  95.     3*
  96.     4*
  97.     5*
  98.     6*
  99.     7*
  100.     8*
  101.     9*
  102.    10*
  103.    11*
  104.    12*
  105.  
  106. [WIN] 14:15:10.13 G:\MIKE\SAMSUL\ANALYSIS >
  107.  
  108. */
  109.  
  110. ///Look in areas near the triple slash for notes regarding the problem
  111.  
  112. #include <iostream.h>
  113. #include <fstream.h>
  114. #include <iomanip.h>
  115. #include <strstream.h>
  116. #include <math.h>
  117.  
  118. /******************************************************************/
  119. // MAIN
  120. main(int argc, char *argv[]) {
  121. register int i=0; //generic counter
  122.  
  123. // OPEN INPUT, OUTPUT FILES
  124. ifstream InpF (argv[1], ios::nocreate);
  125. if (!InpF)  {
  126.   cout << "Problem opening input file " <<argv[1] <<"\n\n";
  127.   return 1;
  128.   }
  129. ofstream OutF("TEST.OUT");
  130. if (!OutF)  {
  131.   cout << "Problem opening output file!\n\n";
  132.   return 1;
  133.   }
  134.  
  135. /******************************************************************/
  136. //READ LINES FROM WEATHER FILE
  137.  
  138. //Inputs
  139. int Loc, Mo;
  140. float WetDays, TotRain, TMax, TMin, Solar, VP, Wind;
  141.  
  142. //Intermediates
  143. float TAvg, VPs, VPd;
  144. char *SBuf, *Output;
  145. long getpointer;
  146. strstream SSBuf(SBuf,100,(ios::in | ios::out));
  147.  
  148. //Outputs:
  149. float Ph20, PSol, PLim;
  150. float H2Oneed, H2Odef;
  151.  
  152. SSBuf.setf (
  153.  ios::right |
  154.  ios::fixed |
  155.  ios::showpoint
  156.  );
  157. SSBuf.width(8);     //How do you set default width???
  158. SSBuf.precision(1); //(don't use (0)--defaults to 6 places!)
  159.                     //How to set precision to 0???
  160. //SSBuf.unsetf(ios::skipws);
  161.  
  162. //Loop thru input file:
  163. while (InpF >>Loc >>Mo >>WetDays >>TotRain >>TMax >>TMin >>Solar >>VP >>Wind
  164.  && OutF) {
  165.  
  166.   ///Have been experimenting with both of these
  167.   ///Seems like they both should do same thing
  168.   InpF.getline(SBuf,10,'\n');
  169.   //InpF.ignore(10,'\n');
  170.  
  171.   //Calculate new variable values:
  172.   TAvg = .5 * (TMax + TMin);
  173.   VPs = 6.108 * exp((17.27 * TAvg)/(237.3+TAvg));
  174.   VPd = (VPs - VP) * .1;  //mbar to kPa
  175.   Ph20 = (TotRain/VPd)* 3.1 * 10 *.001;  //in t/ha
  176.   PSol = (Solar * 30) * 1.2 * 10 *.001;
  177.   if (Ph20 < PSol) PLim = Ph20;  else PLim = PSol; //Limiting factor
  178.   H2Oneed = (PSol * VPd) / (3.1 * 10 * .001);
  179.   H2Odef = H2Oneed - TotRain;
  180.  
  181.   //store in string stream buffer:
  182.   SSBuf.seekp(0);
  183.   SSBuf
  184.    << setw(4) << Loc
  185.    << setw(3) << Mo
  186.    << setprecision(1)
  187.    << setw(6) << TotRain
  188.    << setw(6) << Solar
  189.    << setw(6) << TAvg
  190.    << setprecision(2)
  191.    << setw(6) << VPd
  192.    << setw(8) << Ph20
  193.    << setw(8) << PSol
  194.    << setw(8) << PLim
  195.    << setprecision(1)
  196.    << setw(8) << H2Oneed
  197.    << setw(8) << H2Odef
  198.    << endl;
  199.  
  200.   SSBuf.seekg(0);  //reposition get pointer
  201.   SSBuf.getline(Output,100,'\n');
  202.  
  203.   cout <<setw(5) << ++i <<"*" <<Output << endl;       //output to screen
  204.   ///Note: on running program 2nd time, only i and "*" get output
  205.   OutF << Output << endl;       //Output to file
  206.   }
  207.  
  208. /**********************************************************/
  209. //CLOSE FILES
  210. InpF.close();
  211. OutF.close();
  212.  
  213. /**********************************************************/
  214. return 0;
  215. }
  216.  
  217. /**********************************************************
  218. SAMPLES OF INPUT, OUTPUT:
  219.  
  220.  
  221. INPUT FILE:
  222.  
  223.     1  1   6.40  40.40  14.68   3.45   3.96   9.06   1.42
  224.     1  2   7.45  72.57  13.44   2.55   4.54   8.97   1.59
  225.     1  3   5.30  27.58  18.65   5.45   7.45   9.54   1.68
  226.     1  4   2.75  14.07  26.04  10.42  11.42  10.75   1.97
  227.     1  5   3.65  27.82  30.34  13.18  12.90  11.60   2.46
  228.     1  6   2.15   2.25  34.44  15.48  14.65  12.02   2.51
  229.     1  7    .20    .22  36.45  16.73  14.85  12.93   2.23
  230.     1  8   1.15   2.80  35.15  15.85  12.92  12.48   2.09
  231.     1  9   3.70  12.61  30.89  13.61   9.52  11.96   1.79
  232.     1 10   2.35   7.86  25.15   9.69   7.16  11.08   1.37
  233.     1 11   2.65   9.83  20.05   6.55   5.20  10.10   1.30
  234.     1 12   6.60  30.29  16.02   4.10   3.73   9.39   1.72
  235.  
  236.  
  237. SCREEN OUTPUT OF 1ST RUN
  238.  
  239. [WIN] 14:09:47.66 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
  240. Using input file x.x
  241. Writing output file x
  242.     1*   1  1  40.4   4.0   9.1  0.25    5.07    1.43    1.43    11.4   -29.0
  243.     2*   1  2  72.6   4.5   8.0  0.18   12.83    1.63    1.63     9.2   -63.3
  244.     3*   1  3  27.6   7.4  12.0  0.45    1.89    2.68    1.89    39.2    11.6
  245.     4*   1  4  14.1  11.4  18.2  1.02    0.43    4.11    0.43   135.1   121.1
  246.     5*   1  5  27.8  12.9  21.8  1.45    0.60    4.64    0.60   216.5   188.7
  247.     6*   1  6   2.3  14.6  25.0  1.96    0.04    5.27    0.04   333.2   330.9
  248.     7*   1  7   0.2  14.9  26.6  2.19    0.00    5.35    0.00   377.2   377.0
  249.     8*   1  8   2.8  12.9  25.5  2.02    0.04    4.65    0.04   302.4   299.6
  250.     9*   1  9  12.6   9.5  22.3  1.49    0.26    3.43    0.26   164.6   151.9
  251.    10*   1 10   7.9   7.2  17.4  0.88    0.28    2.58    0.28    73.3    65.5
  252.    11*   1 11   9.8   5.2  13.3  0.52    0.59    1.87    0.59    31.2    21.4
  253.    12*   1 12  30.3   3.7  10.1  0.29    3.19    1.34    1.34    12.7   -17.6
  254.  
  255. [WIN] 14:10:11.33 G:\MIKE\SAMSUL\ANALYSIS >
  256.  
  257.  
  258. SCREEN OUTPUT OF SUBSEQUENT RUNS:
  259.  
  260. [WIN] 14:15:06.72 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
  261. Using input file x.x
  262. Writing output file x
  263.     1*
  264.     2*
  265.     3*
  266.     4*
  267.     5*
  268.     6*
  269.     7*
  270.     8*
  271.     9*
  272.    10*
  273.    11*
  274.    12*
  275.  
  276. [WIN] 14:15:10.13 G:\MIKE\SAMSUL\ANALYSIS >
  277. [or sometimes it just crashes, GPFs, or sez "abnormal program execution"]
  278. */
  279.